package com.server;

import com.google.gson.JsonObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ScanResult extends HttpServlet {

	/**
	 * 签到结果检测
	 * 接收codeStr，sno
	 * 查询数据库，更新数据库签到信息
	 * 返回:
	 * status:
	 * -1  <----> 请求错误
	 *  0  <----> 没选修该课程
	 *  1  <----> 签到成功
	 *  2  <----> 非课程信息
	 * jsonObject:
	 * 		status
	 * 		course_name
	 * 		cid
	 * 		times
	 */

	private String codeStr;
	private String sno;
	private JsonObject jsonObject;
	private int status;
	private static final int REQUEST_ERROR = -1;
	private static final int NOT_LEARN = 0;
	private static final int SUCCESS = 1;
	private static final int NOT_COURSE = 2;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		codeStr = request.getParameter("codeStr");
		sno = request.getParameter("sno");
		jsonObject = new JsonObject();
		System.out.println(codeStr);
		if(codeStr != null && !codeStr.equals("") && sno != null && !sno.equals("")){

			//从codeStr中解析出times和cid并查询数据库有没有该点名信息
			System.out.println(codeStr.length());
			String times = codeStr.substring(codeStr.length() - 2,codeStr.length());
			System.out.println(times);
			String cid = codeStr.substring(5,codeStr.length() - 2);
			System.out.println(cid);
			String sql = "SELECT cid,course_name,time_"+times+" FROM courses where qrCode = '"+codeStr+"' LIMIT 1;";
			System.out.println(sql);
			DBTools db = new DBTools();
			ResultSet rs = db.query(sql);
			DBTools db2 = new DBTools();
			try {
				//rs无返回结果，即不是课程信息
				if(rs == null || !rs.next()){
					status = NOT_COURSE;
				}else if(rs.next()){
					//查询到该点名信息、填充课程数据
					jsonObject.addProperty("course_name", rs.getString("course_name"));
					jsonObject.addProperty("cid", cid);
					jsonObject.addProperty("date", rs.getString(3));
					jsonObject.addProperty("times", times);
					//签到
					String sql2 = "UPDATE student_course SET status_"+times+" = 1 "
							+ "WHERE sno = "+sno+" and "
							+ "cid = '"+cid+"' limit 1";
					//返回值为修改的记录数，0表示没修改，即没选该课
					status = db2.update(sql2);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				db.close();
				db2.close();
			}

		}else{//error
			status = REQUEST_ERROR;
		}

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		jsonObject.addProperty("status", status);
		out.write(jsonObject.toString());
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 *
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
